package com.java.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author jl
 * Created on 2020/9/26
 */
public class NIOFileChannel02 {

    public static void main(String[] args) throws IOException {
        // 创建文件的输入流
        File file = new File("d:\\file01.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        // 通过fileInputStream获取channel
        FileChannel channel = fileInputStream.getChannel();
        // 创建缓冲区
        ByteBuffer buffer = ByteBuffer.allocate((int) file.length());
        // 将通道的数据读入到buffer
        channel.read(buffer);
        // 将buffer的字节数据转成String
        System.out.println(new String(buffer.array()));
        fileInputStream.close();
    }
}
